/**
 * 
 */
package org.xlab.baseframework.web.component;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.xlab.baseframework.common.entity.PageinationEntity;
import org.xlab.baseframework.domain.SystemLog;
import org.xlab.baseframework.domain.query.SystemLogQueryObject;
import org.xlab.baseframework.domain.view.SystemLogVO;
import org.xlab.baseframework.exception.SystemLogNotFoundException;
import org.xlab.baseframework.service.api.ISystemLogService;
import org.xlab.baseframework.system.Declare;

import com.jensjansson.pagedtable.PagedTable;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.AbstractSelect.ItemDescriptionGenerator;
import com.vaadin.ui.Component;

/**
 * @author makubex
 * 
 */
public class LogFunctionLayout extends AbstractBaseVerticalLayout {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = -8443671913237417481L;

	private LogSearchForm logSearchForm;
	private PagedTable logTable;

	@Autowired(required = true)
	@Qualifier("bpf_systemLogService")
	private ISystemLogService systemLogService;

	/**
	 * @param systemLogService
	 *            the systemLogService to set
	 */
	public void setSystemLogService(ISystemLogService systemLogService) {
		this.systemLogService = systemLogService;
	}

	/**
	 * @see com.vaadin.ui.AbstractComponentContainer#attach()
	 */
	public void attach() {
		super.attach();
		SystemLogQueryObject condition = new SystemLogQueryObject();
		condition.setPageination(new PageinationEntity(0, this.logTable
				.getPageLength()));
		this.logSearchForm = new LogSearchForm(
				new BeanItem<SystemLogQueryObject>(condition));
		this.logSearchForm.setVisibleItemProperties(new Object[] {
				SystemLogQueryObject.FIELD_MIN_LOGLEVEL,
				SystemLogQueryObject.FIELD_MAX_LOGLEVEL,
				SystemLogQueryObject.FIELD_MIN_CREATED_TIME,
				SystemLogQueryObject.FIELD_MAX_CREATED_TIME,
				SystemLogQueryObject.FIELD_TITLE,
				SystemLogQueryObject.FIELD_CONTENT });
		super.addComponent(this.logSearchForm);

		try {
			List<SystemLog> logList = this.systemLogService
					.findAllByQueryObject(condition);
			List<SystemLogVO> voList = this.systemLogService
					.convertToVOList(logList);
			this.logTable
					.setContainerDataSource(new BeanItemContainer<SystemLogVO>(
							SystemLogVO.class, voList));
			this.logTable.setVisibleColumns(new Object[] {
					SystemLogVO.FIELD_ID, SystemLogVO.FIELD_S_LOGTYPE,
					SystemLogVO.FIELD_S_LOGLEVEL, SystemLogVO.FIELD_TITLE,
					SystemLogVO.FIELD_CREATED_BY,
					SystemLogVO.FIELD_CREATED_TIME });
			this.logTable.setEditable(false);
			this.logTable
					.setItemDescriptionGenerator(new ItemDescriptionGenerator() {

						/**
						 * @serialField
						 */
						private static final long serialVersionUID = -8755879874532213107L;

						public String generateDescription(Component source,
								Object itemId, Object propertyId) {
							Item item = LogFunctionLayout.this.logTable
									.getItem(itemId);
							return item.getItemProperty(
									SystemLogVO.FIELD_CONTENT).toString();
						}

					});
		} catch (SystemLogNotFoundException e) {
		}
		super.addComponent(this.logTable);
		super.addComponent(this.logTable.createControls());
	}

	/**
	 * <p>
	 * 默认构造函数
	 * </p>
	 */
	public LogFunctionLayout() {
		super();
		super.setMargin(false);
		super.setWidth(Declare.HUNDRED_PERCENT);
		this.logTable = new PagedTable();
		this.logTable.setSizeFull();
	}
}
